package com.starter.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.starter.home.domain.entity.ServeImg;
import com.starter.home.dao.ServeImgDao;
import com.starter.home.domain.vo.FileVo;
import com.starter.home.service.IServeImgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2020-05-01
 */
@Service
public class ServeImgServiceImpl extends ServiceImpl<ServeImgDao, ServeImg> implements IServeImgService {

    @Override
    public void saveImg(Integer id, String name, String path) {
        ServeImg img = new ServeImg();
        img.setInfoId(id);
        img.setImgPath(path);
        img.setImgName(name);
        this.baseMapper.insert(img);
    }

    @Override
    public  List<FileVo> selectById(Integer id) {
        QueryWrapper<ServeImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("info_id",id);
        List<ServeImg> serveImgs = this.baseMapper.selectList(queryWrapper);
        FileVo fileVo = null;
        List<FileVo> list = new ArrayList<>();
        for (int i = 0; i < serveImgs.size(); i++) {
            ServeImg serveImg = serveImgs.get(i);
            fileVo = new FileVo();
            fileVo.setName(serveImg.getImgName());
            fileVo.setUrl(serveImg.getImgPath());
            list.add(fileVo);
        }
        return list;
    }

    @Override
    public int deleteByName(String fileName) {
        QueryWrapper<ServeImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("img_name",fileName);
        int delete = this.baseMapper.delete(queryWrapper);
        return delete;
    }

    @Override
    public List<ServeImg> selectByInfoId(Integer id) {
        QueryWrapper<ServeImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("info_id",id);
        return this.baseMapper.selectList(queryWrapper);
    }
}
